1 class语法糖

1.1 基本结构

  1. 方法不需要function
class Student{
    constructor(name){
        this.name = name;
    }

    study(){
        console.log(`${this.name} is studying`);
    }

    static greeting(){
        console.log('I\'m student.')
    }
}

const stu = new Student('Aeroxian');
stu.study();
// stu.greeting(); error static 方法只能通过类名调用
Student.greeting();

1.2 static修饰方法

  1. static method
  2. static property
class Store{
	static key = 'books';
	
	static displayBooks(){
		console.log(Store.key);
	}
}

2 extends继承

2.1 super调用父亲的构造函数

class Person {
    constructor(firstName, lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
    }
  
    greeting() {
      return `Hello there ${this.firstName} ${this.lastName}`;
    }
  }
  
  class Customer extends Person {
    constructor(firstName, lastName, phone, membership) {
      super(firstName, lastName);
      this.phone = phone;
      this.membership = membership;
    }
  }
  
  const john = new Customer('John', 'Doe', '555-555-5555', 'Standard');
  console.log(john.greeting());